var statisticalChart = new Vue({
    el: '#statisticalChart',
    data: {
       /* 统计类型*/
        type:1,
        //数据统计信息
        option: {

            title: {
                text: '商城数据统计',
                subtext: '采购单位数据统计'
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: []
            },
            toolbox: {
                show: true,
                feature: {
                    /*dataZoom: {
                        yAxisIndex: 'none'
                    },*/
                    /*  dataView: {readOnly: false},*/
                    magicType: {type: ['bar', 'line']},

                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: []
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    formatter: '{value} 万'
                }
            },
            series: [
                /*{
                    name: '家具用具',
                    type: 'bar',
                    data: [0.5, 5, 3, 0.7, 9, 3, 6.5, 1, 8.3, 6, 9, 8],
                    markPoint: {
                        data: [
                            {type: 'max', name: '最大'},
                            {type: 'min', name: '最小'}
                        ]
                    },
                    markLine: {
                        data: [
                            {type: 'average', name: '平均值'}
                        ]
                    }
                },*/

            ]

        },
        //饼状图数据
        optionPie: {
            title: {
                text: '各商家购买金额占比',
                subtext: '饼状图',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                left: 'left',
                data: []
            },
            series: [
                {
                    name: '购买金额（万）',
                    type: 'pie',
                    radius: '55%',
                     center: ['50%', '60%'],
                    data: [

                    ],
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        },
        //时间统计范围
        TimeRange: '',
        //采购单位用户ID
        userId: '',
        //多选框信息
        options: [],
        //商品类型
        goodsType: [],
        //采购单位信息
        userUnit: []


    },

    methods: {
        //post文件下载工具类
        DownLoadFile:function (options) {
            var config = $.extend(true, { method: 'post' }, options);
            var $iframe = $('<iframe id="down-file-iframe" />');
            var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
            $form.attr('action', config.url);
            for (var key in config.data) {
                $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
            }
            $iframe.append($form);
            $(document.body).append($iframe);
            $form[0].submit();
            $iframe.remove();
        },

        downLoadExcel:function () {
            var _this = this;
            if(_this.TimeRange.length<1){
                _this.$message({
                    message: '请选择时间范围！',
                    type: 'success'
                });
                return;
            };
            if(_this.userId==''){
                _this.$message({
                    message: '请选择采购单位！',
                    type: 'success'
                });
                return;
            };
            var data = {
                "userId": _this.userId,
                "startTime":_this.TimeRange[0],
                "overTime":_this.TimeRange[1],
                "type":_this.type
            };

            _this.DownLoadFile({url:"/home/order/unitDownLoadExcel",data:data});

        },


//提交统计条件信息
        create: function () {
            var _this = this;
            if(_this.TimeRange.length<1){
                _this.$message({
                    message: '请选择时间范围！',
                    type: 'success'
                });
                return;
            };
            if(_this.userId==''){
                _this.$message({
                    message: '请选择采购单位！',
                    type: 'success'
                });
                return;
            };
            _this.option.series = [];
            _this.optionPie.series[0].data = [];
            $.post("/home/order/unitTimeStatistics",
                {"userId": JSON.stringify(_this.userId),"startTime":_this.TimeRange[0],"overTime":_this.TimeRange[1],"type":_this.type},
                function (data) {

                    var condition = [];
                    var objectsList = data.data.objectsList;
                    var  goodsType = data.data.goodsType;
                    var legendData = [];
                    for (var j = 0; j < _this.options.length; j++) {
                        legendData.push(_this.options[j].label);
                    }
                    //获取报表各个采购单位名称
                    debugger
                    if(_this.type == 0 || _this.type ==1){
                        _this.option.legend.data = legendData;
                        condition = _this.options;
                    }else{
                        _this.option.legend.data.push(data.data.userName.name);
                        var map = {};
                        map.value = data.data.userName.id;
                        map.label = data.data.userName.name;
                        condition.push(map);
                    }

                    _this.optionPie.legend.data = goodsType;
                    for(var ll=0;ll<_this.optionPie.legend.data.length;ll++){
                        var pieObject = {};
                        pieObject.name =  _this.optionPie.legend.data[ll];
                        pieObject.value = 0;
                        _this.optionPie.series[0].data.push(pieObject);
                    }
                    //获取柱状图X轴的信息
                    _this.option.xAxis.data = goodsType;
                    //定义数据信息

                    for(var i=0; i<objectsList.length;i++){

                        for (var j = 0; j < condition.length; j++) {
                            //饼状图数据信息


                            if(objectsList[i].name ==condition[j].value){

                                var pp={};
                                pp.name = condition[j].label;
                                pp.type = 'bar';
                                pp.data =[];
                                pp.markPoint= {
                                    data: [
                                        {type: 'max', name: '最大'},
                                        {type: 'min', name: '最小'}
                                    ]
                                };
                                pp.markLine = {
                                    data: [
                                        {type: 'average', name: '平均值'}
                                    ]
                                };
                                var numData = 0;

                                for(var a=0; a< goodsType.length;a++){
                                    var  datas = 0;
                                    for(var lp = 0;lp<objectsList[i].val.length;lp++){

                                        if(objectsList[i].val[lp].name == goodsType[a]){
                                            for(var b=0;b<objectsList[i].val[lp].val.length;b++){
                                                _this.optionPie.series[0].data[a].value = _this.optionPie.series[0].data[a].value + Number(objectsList[i].val[lp].val[b].zj)/10000;

                                                datas = datas + Number(objectsList[i].val[lp].val[b].zj);
                                                numData = numData + datas;
                                                continue;
                                            }
                                        }

                                    }
                                    pp.data.push(Number(datas)/10000);

                                    }
                                    continue;
                            }

                        }
                        _this.option.series.push(pp);

                    }


                    //图形实例

                    // 使用刚指定的配置项和数据显示图表。
                    myChart.setOption(statisticalChart.option,true);
                    mainPie.setOption(statisticalChart.optionPie,true);

                });
        }

    },

    created: function () {
        var _this = this;
       /* $.post("/home/order/presentUser",
            "",
            function (data) {
                for (var i = 0; i < data.data.unit.length; i++) {
                    var map = {};
                    map.value = data.data.unit[i].id;
                    map.label = data.data.unit[i].name;
                    _this.options.push(map);
                }
                _this.goodsType = data.data.goodsType;
                _this.userUnit = data.data.unit;
            });
*/



    }


});

var myChart = echarts.init(document.getElementById('main'));
//图形实例
var mainPie = echarts.init(document.getElementById('mainPie'));
// 使用刚指定的配置项和数据显示图表。


